from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import get_user_model
from django.utils.translation import gettext_lazy as _

UserModel = get_user_model()

class ReadersAuthenticationForm(AuthenticationForm):
    username = forms.CharField(
        label=_("Username"), 
        max_length=63,
        widget=forms.TextInput(attrs={
            'class': 'form-control', 
            'placeholder': _('Username'),
            'autofocus': 'autofocus'
        })
    )
    password = forms.CharField(
        label=_("Password"), 
        widget=forms.PasswordInput(attrs={
            'class': 'form-control', 
            'placeholder': _('Password')
        })
    )

    def confirm_login_allowed(self, user):
        """
        确认登录权限。
        如果用户不应该被允许登录，可以在这里抛出一个 ValidationError。
        """
        if not user.is_active:
            raise forms.ValidationError(
                _("This account is currently inactive. Please contact an administrator."),
                code='inactive_account',
            )
        # 如果需要其他检查，可以在这里添加
        # 例如：
        # if user.has_been_banned:
        #     raise forms.ValidationError("You have been banned from logging in.")
        # if user.requires_new_password:
        #     raise forms.ValidationError("Your password has expired.")

        # 如果不需要额外的检查，可以不实现这个方法，或者直接调用父类的方法
        # super().confirm_login_allowed(user)